<?php

namespace McUser\Model;
use Common\Model\CommonModel;

/*
 *  Copyright (c) 2014 The CCP project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a Beijing Speedtong Information Technology Co.,Ltd license
 *  that can be found in the LICENSE file in the root of the web site.
 *
 *   http://www.yuntongxun.com
 *
 *  An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

class McUserVipModel extends CommonModel {

	function _initialize(){}

	protected $trueTableName = 'bg_user_vip';
	/* protected $dbName = 'asheng'; */

	/*  本果会员购买操作  */
	public function oprVipBuy($userID, $vipDaiMa, $vipTitle,$vipPrice, $type, $num, $jinE, $payID, $payMethod){
		
		$data['userID'] = $userID;
		$data['vipDaiMa'] = $vipDaiMa;
		$data['vipTitle'] = $vipTitle;
		$data['vipPrice'] = $vipPrice;

		$data['numBuy'] = $num;
		$data['dateBuy'] = date('Y-m-d H:i:s', time());
		$data['dateStart'] = date('Y-m-d H:i:s', time());
		$data['jinE'] = $jinE;

		$data['payID'] = $payID;
		$data['payMethod'] = $payMethod;

		// 查看是续费还是首次购买.
		$list = $this -> getInfoLast($userID ,$vipDaiMa);
		if($list && count($list) >0){
		    $dateEnd_last = strtotime($list[0]["dateEnd"]);
		    if( date("Y-m-d H:i:s") < $dateEnd_last ){
		        $data['dateStart'] = date('Y-m-d H:i:s', $dateEnd_last); 		//date("Y-m-d H:i:s", strtotime('+1 year', $dateEnd_last));
		    }
		}

		/* 年费 */
		$type = strtoupper($type);
		if ($type == 'Y')
		{
			//$dateEnd = $this ->AddYear($num);
			$data['dateEnd'] = date('Y-m-d H:i:s', strtotime('+1 year', strtotime( $data['dateStart'])));
		}
		else
		{
			/*   月费  */
			//$dateEnd = $this -> AddMonths($num);
			$data['dateEnd'] = date('Y-m-d H:i:s', strtotime('+1 month',$data['dateStart'] ));
		}

		$this -> add($data);
		
	}

	/*
	*	检查 payID 是否存在
	*/
	public function check_payID($payID)	{
		$where['payID'] = $payID;

		$list = $this -> where($where) -> select();

		if ($list)
			return true;
		else 
			return false;

	}

	/**
	 * 检查是否是VIP
	 * @param  [type]  $userID [description]
	 * @return boolean         [description]
	 */
	public function isVip($userID){
		$where['userID'] = $userID;
		$where['dateEnd'] = array('egt',date('Y-m-d'));
		$c = $this->where($where) ->count();

		if( $c > 0)
			return '1';
		else 
			return '0';
	}

	/*   
	*	读出一个会员的所有订阅, 价格高的排在前面.
	*	可以读出多个套餐, 
	*	每个套餐都有最大日期.
	*/
	public function getSubscritionOfUser($userID){
		$where['userID'] = $userID;
		$where['dateEnd'] = array('egt',date('Y-m-d'));
		$list = $this->where($where) -> group('vipDaiMa') -> order('vipDaiMa desc') ->select();

		/*
		dump ($list);
		*/

		return $list;
	}

	public function getInfoLast($userID, $vipDaiMa){
		$where['userID'] = $userID;
		$where['vipDaiMa'] = $vipDaiMa;
		// $where['dateEnd'] = array('egt',date('Y-m-d'));
		$list = $this->where($where) -> order('dateEnd desc') ->select();

		/*
		dump ($list);
		*/

		return $list;
	}

	public function AddDays($days){
		$H=date(H);
		$i=date(i);
		$s=date(s);

		$Y=date(Y);
		$m=date(m);
		$d=date(d);

		$out_date=date( "Y-m-d H:i:s", mktime($H,$i,$s,$m,$d+$days,$Y) );

		return $out_date;
	}

	public function AddMonths($months){
		$H=date(H);
		$i=date(i);
		$s=date(s);

		$Y=date(Y);
		$m=date(m);
		$d=date(d);

		$out_date=date( "Y-m-d H:i:s", mktime($H,$i,$s,$m+$months,$d,$Y) );

		return $out_date;
	}

	public function AddYear($years){
		$H=date(H);
		$i=date(i);
		$s=date(s);

		$Y=date(Y);
		$m=date(m);
		$d=date(d);

		$out_date=date( "Y-m-d H:i:s", mktime($H,$i,$s,$m,$d,$Y+$years) ); 
		return $out_date;
	}


}